#include "setresourcesdlg.h"
#include <QPushButton>
#include <QPlainTextEdit>
#include <QButtonGroup>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include <QDir>

extern QString str_properties;

SetResourcesDlg::SetResourcesDlg(QDialog *parent)
    :QDialog(parent)
{
    setFixedSize(600, 300);

    btn_add = new QPushButton(QObject::tr("add resources"), this);
    btn_cancel = new QPushButton(QObject::tr("cancel"), this);
    btn_ok = new QPushButton(QObject::tr("ok"), this);

    text_resource = new QPlainTextEdit(this);
    text_resource->setFixedSize(540, 200);
    QFont f = text_resource->font();
    f.setPixelSize(16);
    text_resource->setFont(f);

    QHBoxLayout *layout_add = new QHBoxLayout;
    layout_add->addSpacing(30);
    layout_add->addWidget(btn_add, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_add->addStretch();
    layout_add->setContentsMargins(0, 0, 0, 0);

    QHBoxLayout *layout_text = new QHBoxLayout;
    layout_text->addSpacing(30);
    layout_text->addWidget(text_resource, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_text->addSpacing(30);
    layout_text->setContentsMargins(0, 0, 0, 0);

    QHBoxLayout *layout_btn = new QHBoxLayout;
    layout_btn->addStretch();
    layout_btn->addWidget(btn_cancel, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_btn->addWidget(btn_ok, 0, Qt::AlignLeft | Qt::AlignVCenter);
    layout_btn->addSpacing(30);
    layout_btn->setContentsMargins(0, 0, 0, 0);


    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(layout_add);
    mainLayout->addLayout(layout_text);
    mainLayout->addLayout(layout_btn);
    mainLayout->setContentsMargins(0, 0, 0, 0);
    setLayout(mainLayout);
    setContentsMargins(0, 0, 0, 0);

    btn_group = new QButtonGroup(this);
    btn_group->addButton(btn_add, 0);
    btn_group->addButton(btn_cancel, 1);
    btn_group->addButton(btn_ok, 2);

    connect(btn_group, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), [=](QAbstractButton *btn)
    {
        if(btn == btn_add)
        {
            QString dir = QDir::currentPath();
            QString filter = QObject::tr("picture file") + "(*.jpg, *.png, *.gif);;" + QObject::tr("all file") + "(*.*);";
            QString title = QObject::tr("please select a file");
            QString path = QFileDialog::getOpenFileName(this, title, dir, filter);
            if (!path.isEmpty())
            {
                text_resource->appendPlainText(path + ",");
            }
        }
        else if(btn == btn_cancel)
        {
            reject();
        }
        else if (btn == btn_ok)
        {
            str_properties = text_resource->toPlainText();
            accept();
        }
    });

}
